package com.maple.tag;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport{
	private String action;
//	private int currentIndex;
	private int size;
	private Page page;
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
//	public int getCurrentIndex() {
//		return currentIndex;
//	}
//	public void setCurrentIndex(int currentIndex) {
//		this.currentIndex = currentIndex;
//	}


	private JspWriter out;
	private int beginIndex=1;//页面上的页码的开始页码,比如"上一页 11  12 13 14 15 下一页",那就是11
	private int realPageIdentifierCountInPage=PageTagConfig.PAGE_IDENTIFIER_COUNT_IN_PAGE;
	
	@Override
	public int doStartTag() throws JspException {
		calculatePageIndex();
		out=pageContext.getOut();
		return Tag.EVAL_BODY_INCLUDE;
	}
	
	
	@Override
	public int doEndTag() throws JspException {
		try {
			out.write("<form id='tagFormId' action='"+this.action+"' method='post'>");
			
			Enumeration<String> e =this.getValues();
			if(e!=null)
			{
				while(e.hasMoreElements())
				{
					String element=e.nextElement();
					out.write("<input type='hidden' name='"+element+"' value='"+this.getValue(element)+"'>");
				}		
			}
			out.write("</form>");
			out.write("<input id='prePageSize' type='text' name='size' value='"+page.getSize()+"' size='2' onblur='tagFormSubmit(this.value,"+page.getCurrentIndex()+");'>");
			if(PageTagConfig.SHOW_SIZE)
			{
				out.write("每页<input id='pageSize' type='text' name='size' value='"+page.getSize()+"' size='2' onblur='tagFormSubmit(this.value,"+page.getCurrentIndex()+");'>条");
			}
			out.write("<script type='text/javascript'>");
			out.write("		function tagFormSubmit(size,pageIndex)");
			out.write("	{");
			out.write("		  var form=document.getElementById('tagFormId');");
			out.write("	");
			out.write("		   var regExp=/^[\\d]+$/;");
			out.write("		   if(!regExp.test(size)||(size+'').length>2)");
			out.write("	   {");
			out.write("	 	  size=document.getElementById('prePageSize').value;");
			out.write("	   }");
			out.write("	if(form.action.toString().indexOf('?',0)==-1)");
			out.write("	{	");
			out.write("	form.action=form.action+'?page.currentIndex='+pageIndex+'&page.size='+size;");
			out.write("	}");
			out.write("	form.submit();");
			out.write("}");
			out.write("</script>");
			if(PageTagConfig.SHOW_TOTAL_ROW_COUNT)
				out.write(" <span>共"+page.getTotalRowCount()+"条</span>");
			if(PageTagConfig.SHOW_PAGE_COUNT)
				out.write(" <span>共"+page.getPageCount()+"页</span>");	
			if(PageTagConfig.SHOW_CURRENT_PAGE_INDEX)
				out.write(" <span>第"+page.getCurrentIndex()+"页</span>");	
			
			out.write(" <span onclick='tagFormSubmit("+page.getSize()+","+1+")' style='cursor:pointer'>首页</span>");
			out.write(" <span onclick='tagFormSubmit("+page.getSize()+","+(page.getCurrentIndex()>1?page.getCurrentIndex()-1:1)+")' style='cursor:pointer'>上一页</span>");
			for(int i=beginIndex;i<realPageIdentifierCountInPage+beginIndex;i++)
			{
				out.write(" <span onclick='tagFormSubmit("+page.getSize()+","+i+")' style='cursor:pointer'>"+i+"</span>");
			}
			out.write(" <span onclick='tagFormSubmit("+page.getSize()+","+(page.getCurrentIndex()<page.getPageCount()?page.getCurrentIndex()+1:page.getPageCount())+")' style='cursor:pointer'>下一页</span>");
			out.write(" <span onclick='tagFormSubmit("+page.getSize()+","+page.getPageCount()+")' style='cursor:pointer'>尾页</span>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Tag.SKIP_BODY;
	}
	
	
	private void calculatePageIndex()
	{
		int half=PageTagConfig.PAGE_IDENTIFIER_COUNT_IN_PAGE/2;
		
		if(this.page.getPageCount()<PageTagConfig.PAGE_IDENTIFIER_COUNT_IN_PAGE)
		{
			beginIndex=1;
			realPageIdentifierCountInPage=this.page.getPageCount();
		}
		//TODO 这里的算法值得待虑
		else if((this.page.getCurrentIndex()-half)<=1)
		{
			beginIndex=1;
			realPageIdentifierCountInPage=PageTagConfig.PAGE_IDENTIFIER_COUNT_IN_PAGE;
		}
		else if((this.page.getCurrentIndex()+half)>=this.page.getPageCount())
		{
			beginIndex=this.page.getPageCount()-(PageTagConfig.PAGE_IDENTIFIER_COUNT_IN_PAGE-1);
			realPageIdentifierCountInPage=PageTagConfig.PAGE_IDENTIFIER_COUNT_IN_PAGE;
		}
		else
		{
			beginIndex=this.page.getCurrentIndex()-half;
			realPageIdentifierCountInPage=PageTagConfig.PAGE_IDENTIFIER_COUNT_IN_PAGE;
		}
	}

}
